<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 全局搜索
 *
 * @icon fa fa-circle-o
 */
class Qjsosuo extends Backend
{

    /**
     * Qjsosuo模型对象
     * @var \app\admin\model\Qjsosuo
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Qjsosuo;
        $this->view->assign("stypeList", $this->model->getStypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model

                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','opt_date','name','mobile','address','shouli_id','lanzhuang_id','zhuangji_id','stype','createtime']);
                
            }

            $table = 'fa_dxronghe';
            $database = \think\Config::get('database.database');
            $fieldArr = [];
            $list2 = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
            //print_r($list2);
            foreach ($list2 as $k => $v) {
                    $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
            }
            //print_r($fieldArr);;die();
            /*   print_r($list2);die();*/

            $rows = $list->items();
            $input = input();
            $filter = json_decode($input['filter'], true);
            if($filter){

                if(isset($filter['lanzhuang_id']) && $filter['lanzhuang_id']){
                    //揽装人
                    $table_arr = [
                        'fa_dxronghe',//lanzhuang_id
                        'fa_lttongzhao',//lanzhuang
                        'fa_ltronghe',//lanzhuang_id
                        'fa_ltyiji',//lanzhuang_id
                        'fa_ydtietong',//lanzhuang_id
                        'fa_ydchuzhuang',//shouli_id
                        'fa_ydgongyu',//lanzhuang_id
                        'fa_zjxinzhuang',//lanzhuang_id
                        'fa_zjfutong',//lanzhuang_id
                        'fa_zjershou',//lanzhuang_id
                    ];
                }else if(isset($filter['zhuangji_id']) && $filter['zhuangji_id']){
                    //装机人
                    $table_arr = [
                        "fa_ydchuzhuang", //lanzhuang_id
                        "fa_zjxinzhuang", //zhuang_id
                    ];
                }else{
                    $table_arr = [
                        'fa_dxronghe', // rh_date
                        'fa_dxxufei',
                        'fa_dxwangfei',
                        'fa_dxzfshouru',
                        'fa_dxmianfeixian',
                        'fa_lttongzhao',
                        'fa_ltronghe',
                        'fa_lttiaoshifee',
                        'fa_ltxufei',
                        'fa_ltpikou',
                        'fa_ltyiji',
                        'fa_ltqitaincome',
                        'fa_ltzafeifee',
                        'fa_ydtietong',
                        'fa_ydchuzhuang',
                        'fa_ydzfshouru',
                        'fa_ydzaifei',
                        'fa_ydgongyu',
                        'fa_ydgongyuzc',
                        'fa_zjxinzhuang',
                        'fa_zjxufei',
                        'fa_zjfutong',
                        'fa_zjzafei',
                        'fa_zjzafeishouru',
                        'fa_zjershou',
                    ];
                }
                $firstarr = [];
                foreach($table_arr as $v1){
                   if(!empty($filter['lanzhuang_id'])){
                        $where = [];
                        if(!empty($filter['name'])){
                            $where['name'] = $filter['name'];
                        }
                        if(!empty($filter['mobile'])){
                            $where['mobile'] = $filter['mobile'];
                        }
                       if(!empty($filter['address'])){
                           $where['address'] = ['like',"%{$filter['address']}%"];
                       }
                        if($v1=='fa_lttongzhao'){
                            $where['lanzhuang']=$filter['lanzhuang_id'];
                        }else if($v1=='fa_ydchuzhuang'){
                            $where['shouli_id']=$filter['lanzhuang_id'];
                        }else{
                            $where['lanzhuang_id']=$filter['lanzhuang_id'];
                        }
                        $s=Db::table($v1)->where($where)->find();
                       if($v1=='fa_lttongzhao'){
                           $s['lanzhuang_id'] = $s?$s['lanzhuang']:'';
                       }else if($v1=='fa_ydchuzhuang'){
                           $s['lanzhuang_id'] = $s?$s['shouli_id']:'';
                       }
                    }else if(!empty($filter['zhuangji_id'])){
                        $where = [];
                        if(!empty($filter['name'])){
                            $where['name'] = $filter['name'];
                        }
                        if(!empty($filter['mobile'])){
                            $where['mobile'] = $filter['mobile'];
                        }
                       if(!empty($filter['address'])){
                           $where['address'] = ['like',"%{$filter['address']}%"];
                       }
                        if($v1 == 'fa_ydchuzhuang'){
                            $where['lanzhuang_id']=$filter['zhuangji_id'];
                        }else{
                            $where['zhuang_id']=$filter['zhuangji_id'];
                        }
                        $s=Db::table($v1)->where($where)->find();
                       if($v1 == 'fa_ydchuzhuang'){
                           $s['zhuangji_id']=$s?$s['lanzhuang_id']:'';
                           $s['lanzhuang_id'] = $s?$s['shouli_id']:'';
                       }else{
                           $s['zhuangji_id']=$s?$s['zhuang_id']:'';
                       }
                    }else{
                        $where = [];
                        if(!empty($filter['name'])){
                            $where['name'] = $filter['name'];
                        }
                        if(!empty($filter['mobile'])){
                            $where['mobile'] = $filter['mobile'];
                        }
                       if(!empty($filter['address'])){
                           $where['address'] = ['like',"%{$filter['address']}%"];
                       }
                        $s=Db::table($v1)->where($where)->find();
                    }
                    if($s){
                        if(isset($s['name'])){
                            $s['yunying'] = '';
                            if(strpos($v1,'fa_dx')!==false){
                                $s['yunying'] = '电信';
                            }
                            if(strpos($v1,'fa_lt')!==false){
                                $s['yunying'] = '联通';
                            }
                            if(strpos($v1,'fa_yd')!==false){
                                $s['yunying'] = '移动';
                            }
                            if(strpos($v1,'fa_zj')!==false){
                                $s['yunying'] = '广电';
                            }
                            $c = Db::table('fa_auth_rule')->where('name',substr($v1,'3'))->find();
                            $s['caidan'] = $c?$s['yunying'].'-'.$c['title']:'';
                        }
                        $firstarr[] = $s;
                    }
                }
                /* $firstarr[]=[
                     'name'          => '总计: ',
                 ];*/
                if(!empty($firstarr)){
                    $rows = array_merge($rows, $firstarr);
                }
            }

            $result = array("total" => $list->total(), "rows" => $rows);
            return json($result);
        }
        return $this->view->fetch();
    }

}
